package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.bean.SplashResponseBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.SPUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";
    private TextView skip_btn;
    public final String VSERION_CODE = "versionCode";
    private CountDownTimer timer;
    private ImageView splashImg;
    private String splash_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initview();
        splash_img = (String) SPUtil.getData(this, "splash_img", "");
        if (!TextUtils.isEmpty(splash_img)) {
            Glide.with(this).load(splash_img).into(splashImg);
        }
        startTimer(5000);
        NetTool.getApi().getStartImg(NetTool.newParams()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<BaseResponse<SplashResponseBean>>() {
                    @Override
                    public void onData(BaseResponse<SplashResponseBean> value) {
                        Log.i(TAG, "onData: msg=" + value.msg + " getCode=" + value.getCode());
                        if (value.getCode() == 0 && value.getData() != null) {
                            String url = value.getData().getStart_img();
                            if (url.equals(splash_img)) {
                                return;
                            }
                            timer.cancel();
                            SPUtil.saveDate(SplashActivity.this, "splash_img", url);
                            Glide.with(SplashActivity.this).load(url).into(splashImg);
                            if (value.getData().getDistime() != 0) {
                                startTimer(value.getData().getDistime() * 1000);
                            } else {
                                startTimer(5000);
                            }
                        }

                    }
                });

    }

    private void initview() {
        skip_btn = (TextView) findViewById(R.id.btn_skip);
        skip_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                jump();
            }
        });
        splashImg = findViewById(R.id.splash_img);
    }

    private void jump() {
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(Const.PACKGE_NAME, 0);
            int versionCode = packageInfo.versionCode;
            int oldVersionCode = (int) SPUtil.getData(this, VSERION_CODE, -1);
            if (versionCode > oldVersionCode) {
                SPUtil.saveDate(this, VSERION_CODE, versionCode);
                jumpToGuide();
            } else {
                jumpToMain();
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void jumpToGuide() {
        Intent intent = new Intent();
        intent.setClass(this, GuideActivity.class);
        startActivity(intent);
        finish();
    }

    private void jumpToMain() {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void startTimer(int ms) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new CountDownTimer(ms, 1000) {
            @Override
            public void onTick(long l) {
                skip_btn.setText("跳过 " + l / 1000 + " S");
            }

            @Override
            public void onFinish() {
                skip_btn.setText("跳过 0 S");
                jump();
            }
        };
        timer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}
